package com.block.common.interceptor;


import cn.hutool.core.util.StrUtil;
import com.block.common.annotate.NeedToken;
import com.block.common.bean.SessionBean;
import com.block.common.constant.AppRspCodeConstant;
import com.block.common.util.HttpReturnUtil;
import com.block.db.entity.AppEncrypt;
import com.block.db.entity.MemberLoginSession;
import com.block.db.mapper.MemberLoginSessionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * token拦截器 判断是否有 token token是否过期
 */

public class TokenInterceptor implements HandlerInterceptor {
    @Autowired
    RedisTemplate<String, SessionBean> redisSessionTemplate;
    @Resource
    MemberLoginSessionMapper memberLoginSessionMapper;


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if(handler instanceof HandlerMethod)
        {
            HandlerMethod handlerMethod= (HandlerMethod) handler;
            NeedToken needToken=handlerMethod.getMethod().getAnnotation(NeedToken.class);
            String uri= (String) request.getAttribute("uri");
            String lang= (String) request.getAttribute("lang");
            AppEncrypt appEncrypt=(AppEncrypt) request.getAttribute("appEncrypt");
            //无拦截器 不需要验证token
            if(needToken==null){
                return true;
            }

            String token=null;
            if(needToken.source()==1){
                token= (String) request.getAttribute("token");
            }else{
                token = request.getParameter("token");
            }

            //不必须有token
            if(needToken.need()==false&&StrUtil.isEmpty(token)){
                SessionBean sessionBean=new SessionBean();
                sessionBean.setUid(0L);
                request.setAttribute("sessionBean",sessionBean);
                request.setAttribute("token","");
                return  true;
            }

            //token为空 返回token为空 用户需要登录
            if(StrUtil.isEmpty(token)){
                HttpReturnUtil.returnJson(response,appEncrypt,AppRspCodeConstant.TOKEN_ISNULL,null,new Date(),uri);
                return false;
            }
            SessionBean sessionBean  =  redisSessionTemplate.opsForValue().get(token);
            if(sessionBean==null){
                MemberLoginSession memberLoginsession=new MemberLoginSession();
                memberLoginsession.setToken(token);
                memberLoginsession.setStatus("TIME_OUT");
                memberLoginSessionMapper.updateById(memberLoginsession);
                HttpReturnUtil.returnJson(response,appEncrypt,AppRspCodeConstant.TOKEN_ISNULL,null,new Date(),uri);
                return false;
            }
            request.setAttribute("sessionBean",sessionBean);
        }
        return true;
    }
}
